package com.ruoyi.tea.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.tea.cache.SeeCache;
import com.ruoyi.tea.domain.TeaRecoring;
import com.ruoyi.tea.domain.TeaSeeMedia;
import com.ruoyi.tea.domain.TeaUser;
import com.ruoyi.tea.service.ITeaRecoringService;
import com.ruoyi.tea.service.ITeaSeeMediaService;
import com.ruoyi.tea.service.ITeaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/***
 * 录播接口
 * @author wangfeng
 * @time 2022年08月06日00:26:14
 */
//TODO 该位置需要考虑查库、插库、更新导致问题锁表问题
@Controller
@RequestMapping("/wx/api/room/recording")
public class RecordingApiController extends BaseController {

    @Autowired
    private ITeaUserService iTeaUserService;

    @Autowired
    private ITeaSeeMediaService iTeaSeeMediaService;

    @Autowired
    private ITeaRecoringService iTeaRecoringService;

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody TeaRecoring teaRecoring) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaRecoring.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(500);
            tableDataInfo.setMsg("未登录");
            return tableDataInfo;
        }
        startPage();
        teaRecoring.setStatus(2);
        List<TeaRecoring> list = iTeaRecoringService.selectTeaRecoringList(teaRecoring);
        return getDataTable(list);
    }

    @RequestMapping("/follow/list")
    @ResponseBody
    public TableDataInfo followList(@RequestBody TeaRecoring teaRecoring) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaRecoring.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(500);
            tableDataInfo.setMsg("未登录");
            return tableDataInfo;
        }
        startPage();
        List<TeaRecoring> list = iTeaRecoringService.selectTeaRecoringList(teaRecoring);
        return getDataTable(list);
    }

    /**
     * 保存直播观看记录
     *
     * @param teaSeeMedia
     * @return
     */
    @RequestMapping("/see/history/save")
    @ResponseBody
    public AjaxResult seeMediaHsitorySave(@RequestBody TeaSeeMedia teaSeeMedia) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaSeeMedia.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            return AjaxResult.error("未登录");
        }
        teaSeeMedia.setUserName(teaUserList.get(0).getName());
        teaSeeMedia.setNickName(teaUserList.get(0).getNickName());
        teaSeeMedia.setPhone(teaUserList.get(0).getPhone());
        teaSeeMedia.setUserCode(teaUserList.get(0).getUserCode());
        teaSeeMedia.setStartTime(new Date());
        iTeaSeeMediaService.insertTeaSeeMedia(teaSeeMedia);
        return AjaxResult.success();
    }


    /**
     * 心跳进行结束时间更新
     * 没10秒更新一次
     *
     * @param teaSeeMedia
     * @return
     */
    @RequestMapping("/see/history/heartbeat")
    @ResponseBody
    public AjaxResult seeMediaHeartbeat(@RequestBody TeaSeeMedia teaSeeMedia) {
        teaSeeMedia.setCreateTime(null);
        List<TeaSeeMedia> teaSeeMedias = iTeaSeeMediaService.selectTeaSeeMediaList(teaSeeMedia);
        if (teaSeeMedias != null && teaSeeMedias.size() != 0) {
            TeaSeeMedia update = new TeaSeeMedia();
            update.setId(teaSeeMedias.get(teaSeeMedias.size() - 1).getId());
            update.setEndTime(new Date());
            iTeaSeeMediaService.updateTeaSeeMedia(update);
        }
        return AjaxResult.success();
    }

    @RequestMapping("/info")
    @ResponseBody
    public AjaxResult info(@RequestBody TeaRecoring teaRecoring) {
        TeaUser teaUser = new TeaUser();
        teaUser.setOpenId(teaRecoring.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(teaUser);
        if (teaUserList == null || teaUserList.size() == 0) {
            return AjaxResult.error("未登录");
        }
        return AjaxResult.success(iTeaRecoringService.selectTeaRecoringApiById(teaRecoring));
    }

    @RequestMapping("/number")
    @ResponseBody
    public AjaxResult seeNumber(@RequestBody TeaRecoring teaRecoring) {
        return AjaxResult.success(iTeaRecoringService.seeNumber(teaRecoring));
    }

    @RequestMapping("/see/history")
    @ResponseBody
    public TableDataInfo seeHistory(@RequestBody TeaUser teaUser) {
        TeaUser tmp = new TeaUser();
        tmp.setOpenId(teaUser.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(tmp);
        if (teaUserList == null || teaUserList.size() == 0) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(500);
            tableDataInfo.setMsg("未登录");
            return tableDataInfo;
        }
        startPage();
        List<TeaRecoring> list = iTeaSeeMediaService.seeHsitory(teaUser);
        return getDataTable(list);
    }
}
